package com.joysuch.wwyt.datasync.service.impl;

import com.joysuch.wwyt.core.support.Context;
import com.joysuch.wwyt.datasync.entity.DataSyncInfo;
import com.joysuch.wwyt.datasync.repository.DataSyncInfoDao;
import com.joysuch.wwyt.datasync.service.DataSyncGenerateDataService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;


@Service
public class DataSyncGenerateDataServiceImpl implements DataSyncGenerateDataService {

    @Autowired
    private DataSyncInfoDao dataSyncInfoDao;

    @Value("${wwyt.feature.datasync.enabled}")
    private boolean featureDataSyncEnabled;
    @Value("${wwyt.feature.datasync.customer}")
    private String dataSyncCustomer;



    @Override
    public void insert(String tableName, Long dataId, String changeType, String primaryKey) {
         // 开关关闭则不保存数据
        if(!featureDataSyncEnabled || "xinyuan".equals(dataSyncCustomer)){
            return;
        }

        DataSyncInfo info = new DataSyncInfo();
        info.setTableName(tableName);
        info.setDataId(dataId);
        info.setPrimaryKey(primaryKey);
        info.setChangeType(changeType);
        info.setOrgCode(Context.getCurrentOrgCode());
        info.setTenentId(Context.getCurrentTenantId());
        info.setState((byte) 0);
        save(info);
    }

    @Override
    public void save(DataSyncInfo dataSyncInfo) {
        dataSyncInfoDao.save(dataSyncInfo);
    }
}
